using System;
using System.Collections.Generic;
using BitMango; 
using Data;
using Holoville.HOTween;
using SevenPieceGame;
using UnityEngine;

public class IntroSceneHandler : MonoBehaviour, IGameScene, IHandlerBase
{
    private void Start()
    {
        this.btnEdit.SetActive(false);
    }

    public void DoDataExchange()
    {
        this.finger.SetActive(false);
        if (DataContainer<Data.Root>.Data.tutorialdata.record.isFirst)
        {
            Scene.QueuePopup("TutorialPopup", true, delegate (object close)
            {
            });
            DataContainer<Data.Root>.Data.tutorialdata.record.isFirst = false;
            DataContainer<Data.Root>.Data.tutorialdata.SaveContext();
            DataContainer<Data.Root>.Data.options.SaveContext();
            DataContainer<Data.Root>.Data.userdata.record.isFirstVersion = true;
            DataContainer<Data.Root>.Data.userdata.SaveContext();
        }
        else
        {
            this.DailyReward();
            if (DataContainer<Data.Root>.Data.tutorialdata.record.isFirstFreeCoin)
            {
                this.finger.SetActive(true);
            }
            //if (!PlayerPrefs.HasKey("StarterPack"))
            //{
            //    this.starterInfo.ShowStarterPack();
            //    PlayerPrefs.SetString("StarterPack", "OK");
            //    PlayerPrefs.Save();
            //}
        }
        SyncPackage.Sync();
        this.ReturnedLog();
    }

    public void OnEnterScene(object param)
    {
        this.top.transform.localPosition = new Vector3(this.top.transform.localPosition.x, BlockBannerAnchor.GetTopPositionY(), this.top.transform.localPosition.z);
        float bannerPositionY = BlockBannerAnchor.GetBannerPositionY();
        this.bottom.transform.localPosition = new Vector3(this.bottom.transform.localPosition.x, bannerPositionY, this.bottom.transform.localPosition.z);
        MainManager.Instance.CheckShowBtnShop();
    }

    public void OnLeaveScene()
    {
    }

    private void ReturnedLog()
    {
        DateTime d = DateTime.UtcNow;
        if (PlayerPrefs.HasKey("installed"))
        {
            d = DateTime.Parse(PlayerPrefs.GetString("installed"));
        }
        TimeSpan timeSpan = DateTime.Now - d;
        if (timeSpan.Days == 1)
        {
            SingletonMonoBehaviour<Director>.Instance.AppsFlyerLogEvent(string.Format("af_d{0}", timeSpan.Days));
        }
        if (timeSpan.Days >= 2 && timeSpan.Days <= 7 && PlayerPrefs.GetInt(string.Format("af_d{0}", timeSpan.Days - 1), 0) != 0)
        {
            SingletonMonoBehaviour<Director>.Instance.AppsFlyerLogEvent(string.Format("af_d{0}", timeSpan.Days));
        }
    }

    private void OnClickMultiple2()
    {
        BMSoundHelper.Play("se_btnclick", false, 1f);
        GameMode.packageMode = "multiple";
        this.GotoPlayOrPackage();
    }

    private void OnClickCollection()
    {
        BMSoundHelper.Play("se_btnclick", false, 1f);
        DWATTHelper.logEvent("Collection", new object[]
        {
            "Enter"
        });
        Scene.ChangeTo("CollectionThemeScene", "IntroScene");
    }

    private void OnClickEdit()
    {
        BMSoundHelper.Play("se_btnclick", false, 1f);
        GameMode.gameMode = GameType.TRIANGLE;
        BlocksCoordinate.coordinate = new BlockTriangleCoordinate();
        Scene.ChangeTo("EditScene", null);
    }

    private void OnClickSetting()
    {
        BMSoundHelper.Play("se_btnclick", false, 1f);
        Scene.QueuePopup("SettingPopup", false);
    }

    private void OnClickReview()
    {
        BMSoundHelper.Play("se_btnclick", false, 1f);
        //UncleBill.OpenStore(null, true);
    }

    private void OnClickStore()
    {
        BMSoundHelper.Play("se_btnclick", false, 1f);
        // Scene.QueuePopup("StorePopup");
        // todo show no money
    }

    private void GotoPlayOrPackage()
    {
        string @string = PlayerPrefs.GetString(string.Format("FIRSTPLAY_{0}", GameMode.packageMode), "TRUE");
        if (@string.Equals("TRUE"))
        {
            PlayerPrefs.SetString(string.Format("FIRSTPLAY_{0}", GameMode.packageMode), "FALSE");
            PlayerPrefs.Save();
            for (int i = 0; i < DataContainer<Data.Root>.Data.packages.Length; i++)
            {
                if (DataContainer<Data.Root>.Data.packages[i].mode.Equals(GameMode.packageMode))
                {
                    DataContainer<Data.Root>.Data.currentPackage = DataContainer<Data.Root>.Data.packages[i];
                    DataContainer<Data.Root>.Data.currentLevel = DataContainer<Data.Root>.Data.currentPackage.levels[0];
                    Scene.ChangeTo("PlayScene", null);
                    return;
                }
            }
        }
        else
        {
            Scene.ChangeTo("PackageScene", null);
        }
    }

    /*private void OnClickShopBtn()
    {
        if (UserLayerManager.Instance.IsInstallOrLowUser())
        {
            MainManager.Instance.ShowUIShop();
        }
        else
        {
            Scene.ShowPopup("StorePopup", null, null);
        }
    }*/
    
    private void OnClickGift()
    {
        this.finger.SetActive(false);
    }

    private void DailyReward()
    {
        if (BlockAlarm.IsValidDailyReward())
        {
            Scene.QueuePopup("AttendPopup", null, delegate (object result)
            {
                this.OnCloseAttendPopup();
            });
        }
    }

    private void OnCloseAttendPopup()
    {
        BlockAlarm.RegisterAlarm();
    }

    private void OnEscape()
    {
        Scene.QueuePopup("ExitPopup");
    }

    [SerializeField]
    private tk2dCamera cam;

    [SerializeField]
    private GameObject curtain;

    public GameObject playBtn;

    public GameObject topDown;

    public GameObject bottomUp;

    public GameObject btnEdit;

    public GameObject top;

    public GameObject bottom;

    public GameObject finger;

    public StarterInfo starterInfo;

    private bool onAuthenicate;

    private Tweener tweener;

    private bool isAutoScroll;

    private float prevValue;

    private float currentValue;

    private List<int> orderIndexs = new List<int>();

    private string[] modes = new string[]
    {
        "triangle",
        "square",
        "hexa",
        "rhombus",
        "tangram",
        "multiple"
    };
}
